package com.lz.ccjdemo.act;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.lz.ccjdemo.R;
import com.lz.ccjdemo.greendao.DaoUtils;
import com.lz.ccjdemo.greendao.data.DishData;
import com.lz.ccjdemo.greendao.data.OperationProcessData;
import com.lz.ccjdemo.greendao.data.WarehouseData;
import com.lz.ccjdemo.utils.Tools;

import java.util.ArrayList;
import java.util.List;

/**
 * 录制流程
 */
public class RecordingProcessActivity extends Activity {

    List<RecordingData> list = new ArrayList<>();
    List<RecordingData> playList = new ArrayList<>();

    public class RecordingData {
        public int id;//操作id
        public int viewId;//被操作的viewId  正常流程应当存储操作指令 当前用viewId只是替代
        public String set;
        public long startTime;//当前步骤开始时间
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recording_process);
    }

    private long lastTime = 0;

    private void addSet(String set, int viewId) {
        if (list == null) {
            list = new ArrayList<>();
        }
        long time = lastTime == 0 ? 0 : System.currentTimeMillis() - lastTime;
        RecordingData recordingData = new RecordingData();
        recordingData.id = list.size();
        recordingData.viewId = viewId;
        recordingData.set = set;
        recordingData.startTime = time;
        list.add(recordingData);
        lastTime = System.currentTimeMillis();

        Toast.makeText(this, set + "距离上一步时间：" + time+"ms", Toast.LENGTH_SHORT).show();
    }

    public void clickStart(View view) {
        //开启录制
        list.clear();
        lastTime = System.currentTimeMillis();
        addSet("开始节点", view.getId());
    }

    public void clickSet(View view) {
        //步骤点击
        if (view.getTag() == null) return;
        addSet((String) view.getTag(), view.getId());
    }

    public void clickEnd(View view) {
        //结束录制
        addSet("结束", view.getId());
        playList.clear();
        playList.addAll(list);
        list.clear();
    }

    private void addSql(){

        DishData dishData=new DishData();
        dishData.setDish_name("辣椒炒肉");
        long dishId= DaoUtils.getInstance().insertDish(dishData);
        for (int i = 0; i < 4; i++) {
            WarehouseData warehouseData=new WarehouseData();
            warehouseData.setWarehouse_name("name"+i);
            warehouseData.setWarehouse_number("number"+i);
            warehouseData.setContent("context"+i);
            warehouseData.setDish_id(dishId);
            DaoUtils.getInstance().insertWarehouse(warehouseData);
        }
        for (int i = 0; i < 6; i++) {
            OperationProcessData processData=new OperationProcessData();
            processData.setStir_gear("攪拌"+i);
            processData.setTemperature_gear("溫度"+i);
            processData.setWarehouse_id("a"+i);
            processData.setSeasoning("調料搭配"+i);
            processData.setThicken_with_starch("勾芡"+i);
            processData.setDish_id(dishId);
            DaoUtils.getInstance().insertOperationProcess(processData);
        }
    }

    private int playPosition = 0;

    public void clickPlay(View view) {
        //播放录制的内容
        playPosition = 0;
        loadSet();
    }

    private void loadSet() {
        if (playList == null || playList.size() == 0) {
            addSql();
            return;
        }
        RecordingData data = playList.get(playPosition);
        View clickView = findViewById(data.viewId);
        clickView.performClick();
        if (handler == null) {
            handler = new Handler();
        }
        if (playList.size() > playPosition + 1) {
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    loadSet();
                }
            }, playList.get(playPosition + 1).startTime);
        }
        playPosition++;
    }

    Handler handler;
}
